package com.cgc.venus.server.core.k8s.impl;

import com.cgc.venus.server.core.bean.response.VenusResponseStatus;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.core.k8s.inter.K8sNodeService;
import com.cgc.venus.server.core.utils.StringUtil;
import io.fabric8.kubernetes.api.model.Node;
import io.fabric8.kubernetes.api.model.NodeList;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.cgc.venus.server.core.utils.appearNumber.appearNumber;

/**
 * Created by xiaobin on 2017/9/19.
 */
@Service
public class K8sNodeServiceImpl implements K8sNodeService {
    @Autowired
    private KubernetesClient k8sClient;
//    获取节点信息
    @Override
    public List<Node> nodeDataQuery() throws VenusException{
        List<Node> nodeList=new ArrayList<Node>();
        nodeList=k8sClient.nodes().list().getItems();
        if(nodeList.size()==0){
            throw new VenusException(VenusResponseStatus.K8S_EXCEPTION,"查询节点信息失败");

        }
        return nodeList;
    }
//设置node标签

    public boolean nodeLabelSet(String nodeName,Map<String,String> labels) throws VenusException{
        if(StringUtil.isEmpty(nodeName)){
            throw new VenusException(VenusResponseStatus.K8S_EXCEPTION,"nodename信息为空");
        }
        if(labels.isEmpty()){
            throw new VenusException(VenusResponseStatus.K8S_EXCEPTION,"labels信息为空");
        }
        Node node=new Node();
        node=k8sClient.nodes().withName(nodeName).edit().editMetadata().addToLabels(labels).endMetadata().done();
        String r=node.getMetadata().getLabels().get("physical.group");
        if(StringUtil.isEmpty(r)){
            throw new VenusException(VenusResponseStatus.K8S_EXCEPTION,"异常，设置分区失败");
        }

        return true;
    }
//获取物理分组的node标签

    public List<String> nodeLabel() throws VenusException{
//        Node node=new Node();
        String labelName="physical.group";//指定获取该组的值

        List<Node> nodeList=new ArrayList<Node>();
        nodeList=k8sClient.nodes().list().getItems();
        List<String> labeList=new ArrayList<String>();
        for(Node node:nodeList) {
            String r=node.getMetadata().getLabels().get(labelName);
            labeList.add(r);
        }
        if(labeList.size()==0){
            throw new VenusException(VenusResponseStatus.K8S_EXCEPTION,"获取节点信息失败");
        }
        return labeList;

    }
//查询k8s标签下的节点信息
public NodeList nodeQueryByLabel(String label) throws VenusException{
    NodeList nodeList=new NodeList();
    nodeList=k8sClient.nodes().withLabelIn("physical.group",label).list();
    return nodeList;
}

public int podNumber(String nodeName) throws VenusException{

    List<Pod> podArrayList=new ArrayList<Pod>();
    podArrayList=k8sClient.pods().list().getItems();

    if(podArrayList.size()==0||podArrayList==null){
        throw new VenusException(VenusResponseStatus.K8S_EXCEPTION,"获取pod信息失败");
    }
    List<String> stringList=new ArrayList<String>();
    int num=0;
    for(int i=0;i<podArrayList.size();i++){
        String ab=podArrayList.get(i).toString();
        stringList.add(ab);
    }
    String  aab =stringList.toString();
    String findText = "nodeName="+nodeName;
    num = appearNumber(aab, findText);
    return num;
}

    public List<Pod> getPods() throws VenusException{
        return  k8sClient.pods().list().getItems();
    }


}


